//
//  RightCollectionView.m
//  MJMulMenu
//
//  Created by HuiJiaHuDong on 2017/9/8.
//  Copyright © 2017年 HuiJia. All rights reserved.
//

#import "RightCollectionView.h"
#import "MJColHeaderView.h"
#import "RightColViewCell.h"

UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader NS_AVAILABLE_IOS(6_0);

@interface RightCollectionView()<UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout>

@property(nonatomic , assign) BOOL  isOneSection;

@end

@implementation RightCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
	if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
		
		//注册
		[self registerNib:[UINib nibWithNibName:NSStringFromClass([RightColViewCell class]) bundle:nil] forCellWithReuseIdentifier:colID];
		
		[self registerNib:[UINib nibWithNibName:NSStringFromClass([MJColHeaderView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:colHeaderID];
		
		self.isOneSection = NO;
		
		self.backgroundColor = [UIColor whiteColor];
		
		self.dataSource = self;
		self.delegate = self;
		
	}
	
	return self;
}


#pragma mark-- 设置数据(过来的已经是组数据)
- (void)setColDataArr:(NSArray<MJMenuItem *> *)colDataArr
{
	_colDataArr = colDataArr;
	
	//修复bug,当没有数据的时候,依然需要刷新colView,不然会造成数据混乱,甚至奔溃
	if (!colDataArr || colDataArr.count ==                                                                                                                                                                                                                                                                                                                                             0)
	{
		
		[self reloadData];
		
		return;
	};
	
	for (MJMenuItem *rowItem in colDataArr) {
		
		if (!rowItem || !rowItem.nextArray ||rowItem.nextArray.count == 0) continue;
		
		//计算差多少个
		NSInteger surplusNum = rowItem.nextArray.count % 3;
		
		if (surplusNum <=0) continue;
		
		NSInteger lackNum = 3 - surplusNum;
		
		//补全差的模型数
		for (NSInteger i = 0; i < lackNum; i++) {
			MJMenuItem *item = [[MJMenuItem alloc] init];
			
			[rowItem.nextArray addObject:item];
		}
		
	}
	
	
	[self reloadData];
	
	//滚动有问题暂时不修复,启用需要修复
//	MJMenuItem *rowItem = colDataArr[0];
//	
//	//滚回顶部
//	NSIndexPath *path = [NSIndexPath indexPathForRow:rowItem.nextArray.count - 1 inSection:0];
//	
//	//UICollectionViewScrollPositionCenteredVertically这个是滚动到header的头部.而top是当前组的最头部
//	[self scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
	//判断是否是多组
	if (!self.colDataArr || self.colDataArr.count == 0) return 0;
	
	return  self.colDataArr.count;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
	//取出每组的数据,返回每组中的row个数
	if (section > self.colDataArr.count - 1) return 0;
	
	//单独组中行模型
	MJMenuItem *rowMenuItem = self.colDataArr[section];
	
	if (!rowMenuItem || !rowMenuItem.nextArray) return 0;
	
	return rowMenuItem.nextArray.count;
}

static NSString *const colID = @"RightColViewCellID";

static NSString *const colHeaderID = @"MJColHeaderViewID";

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
	RightColViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:colID forIndexPath:indexPath];
	
	//取出行数据
	//取出每组的数据,返回每组中的row个数
	if (!self.colDataArr || indexPath.section > self.colDataArr.count - 1) return 0;
	
	//单独组中行模型
	MJMenuItem *rowMenuItem = self.colDataArr[indexPath.section];
	
	if (!rowMenuItem || !rowMenuItem.nextArray) return 0;
	
	MJMenuItem *item = rowMenuItem.nextArray[indexPath.row];
	
	cell.menuItem = item;
	
	
	return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
	
	NSString *reuseIdentifier;
	if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
		reuseIdentifier = @"footer";
	}else{
		reuseIdentifier = colHeaderID;
	}
	MJColHeaderView *headerView =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
	
	if (!self.colDataArr || indexPath.section > self.colDataArr.count) return headerView;
	
	MJMenuItem *item = self.colDataArr[indexPath.section];
	
	headerView.headerTitle.text = item.meunName;
	
	
	
	return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
	CGSize size= { self.bounds.size.width , 44 };
	
	return size;
}

#pragma mark-- 点击代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
	if ([self.clickDelegate respondsToSelector:@selector(rightCollectionView:andClickedIndex:)]) {
		
		[self.clickDelegate rightCollectionView:self andClickedIndex:indexPath];
	}
}









@end
